package com.wandian.texas;

import java.io.IOException;

/**
 * @author long
 * @Create 2012-10-19 上午10:40:20
 * @QQ 108232706
 * @version 1.0
 * @description IoHandler
 */
public interface IoHandler {
	/**
	 * Invoked from an I/O processor thread when a new connection has been
	 * created. Because this method is supposed to be called from the same
	 * thread that handles I/O of multiple sessions, please implement this
	 * method to perform tasks that consumes minimal amount of time such as
	 * socket parameter and user-defined session attribute initialization.
	 */
	void sessionCreated(SessionObject session) throws Exception;

	/**
	 * Invoked when a connection has been opened. This method is invoked after
	 * {@link #sessionCreated(SessionObject)}. The biggest difference from
	 * {@link #sessionCreated(SessionObject)} is that it's invoked from other
	 * thread than an I/O processor thread once thread model is configured
	 * properly.
	 */
	void sessionOpened(SessionObject session) throws Exception;

	/**
	 * Invoked when a connection is closed.
	 */
	void sessionClosed(SessionObject session) throws Exception;

	/**
	 * Invoked with the related {@link IdleStatus} when a connection becomes
	 * idle. This method is not invoked if the transport type is UDP; it's a
	 * known bug, and will be fixed in 2.0.
	 */
	void sessionIdle(SessionObject session, IdleStatus status) throws Exception;

	/**
	 * Invoked when any exception is thrown by user {@link IoHandler}
	 * implementation or by MINA. If <code>cause</code> is an instance of
	 * {@link IOException}, MINA will close the connection automatically.
	 */
	void exceptionCaught(SessionObject session, Throwable cause) throws Exception;

	/**
	 * Invoked when a message is received.
	 */
	void messageReceived(SessionObject session, Object message) throws Exception;

	/**
	 * Invoked when a message written by {@link SessionObject#write(Object)} is
	 * sent out.
	 */
	void messageSent(SessionObject session, Object message) throws Exception;
}
